package com.hongshan.wallet.core.home

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.support.v7.app.AlertDialog
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.hongshan.wallet.BR
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.core.InputPwdDialogFragment
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.util.goSetPwd
import kotlinx.android.synthetic.main.appbar.*

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/3/27
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */

@Route(path = RouterIndex.WITHDRAWAL)
class WithdrawalActivity : MyActivity<ViewDataBinding, WithdrawalViewModel>() {

    val llMenu by lazy { LinearLayout(this) }
    val bottomSheetDialog by lazy { BottomSheetDialog(this) }
    val inputPwdDialogFragment by lazy { InputPwdDialogFragment() }

    override fun initVariableId() = BR.vm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_withdrawal

    override fun initViewModel() = ViewModelProviders.of(this)[WithdrawalViewModel::class.java]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        llMenu.orientation = LinearLayout.VERTICAL
        tvTitle.text = "提现"
        bottomSheetDialog.setContentView(llMenu)
        viewModel.getBankCard()
        inputPwdDialogFragment.action = "提现"
        inputPwdDialogFragment.inputComplete = {
            viewModel.checkPwd(it)
        }
        viewModel.showPwdDialog.observe(this, Observer {
            inputPwdDialogFragment.money = "￥${viewModel.money.get() ?: "0"}"
            inputPwdDialogFragment.show(supportFragmentManager, javaClass.name)
        })
        viewModel.hidePwdDialog.observe(this, Observer {
            inputPwdDialogFragment.dismiss()
        })
        viewModel.showBankCard.observe(this, Observer {
            llMenu.removeAllViews()
            viewModel.bankList.forEach {
                bankItemView(it.bank + it.card_type_name + it.card_no, it.bank_img, View.OnClickListener { _ ->
                    viewModel.bankName.set(it.bank)
                    viewModel.bankId = it.id?.toString() ?: ""
                    viewModel.bankImg.set(it.bank_img)
                    bottomSheetDialog.hide()
                })
            }
            bankItemView("添加银行卡", null, View.OnClickListener {
                go(RouterIndex.ADD_CARD)
                bottomSheetDialog.hide()
            })
            bottomSheetDialog.show()
        })
        viewModel.showErrorDialog.observe(this, Observer {
            showErrorDialog()
        })
    }

    private fun bankItemView(text: String, bank_img: String?, click: View.OnClickListener) {
        val tv = TextView(this)
        val imageView = ImageView(this)
        val ll = LinearLayout(this)
        ll.orientation = LinearLayout.HORIZONTAL
        ll.gravity = Gravity.CENTER_VERTICAL
        ll.setPadding(40, 30, 40, 30)
        tv.setPadding(40, 0, 0, 0)
        tv.text = text
        tv.setSingleLine()
        ll.setOnClickListener(click)
        val params = LinearLayout.LayoutParams(100, 100)
        Glide.with(imageView).load(bank_img).into(imageView)
        ll.addView(imageView, params)
        ll.addView(tv)
        llMenu.addView(ll)
    }

    fun showErrorDialog() {
        AlertDialog.Builder(this)
            .setTitle("支付密码输入错误")
            .setPositiveButton("重新输入") { dialog, _ ->
                inputPwdDialogFragment.cleanPwd()
                dialog.dismiss()
            }
            .setNegativeButton("找回密码") { dialog, _ ->
                dialog.dismiss()
                inputPwdDialogFragment.dismiss()
                goSetPwd()
            }
            .show()
    }
}